package com.tianhui.consignortwofei.mvp.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.other.CommonEvent;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.google.gson.Gson;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.GoodsModel;
import com.tianhui.consignortwofei.mvp.model.enty.BoHuiBodyBean;
import com.tianhui.consignortwofei.mvp.model.enty.GoodsInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.lite.CarSearchAuditActivity;
import com.tianhui.consignortwofei.other.EventWhat;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/28
 * @description
 **/
public class FindCarTuiSongActivity extends BaseActivity {

//    @BindView(R.id.activity_about_appNameTextView)
//    TextView mAppNameTextView;
//    @BindView(R.id.activity_about_appVersionTextView)
//    TextView mAppVersionTextView;
//    @BindView(R.id.activity_about_updateLinearLayout)
//    LinearLayout mUpdateLinearLayout;


    @BindView(R.id.tv_zhuanghuo_text)
    TextView tvZhuanghuoText;
    @BindView(R.id.tv_kufang_text)
    TextView tvKufangText;
    @BindView(R.id.tv_mudi_text)
    TextView tvMudiText;
    @BindView(R.id.tv_zhongliang_text)
    TextView tvZhongliangText;
    @BindView(R.id.tv_dunweifei_text)
    TextView tvDunweifeiText;
    @BindView(R.id.tv_dunzhuanche_text)
    TextView tvDunzhuancheText;
    @BindView(R.id.tv_huoquan_text)
    TextView tvHuoquanText;
    @BindView(R.id.tv_duncangchu_text)
    TextView tvDuncangchuText;
    @BindView(R.id.tv_qita_text)
    TextView tvQitaText;
    @BindView(R.id.tv_time_text)
    TextView tvTimeText;
    @BindView(R.id.tv_status_text)
    TextView tvStatusText;

    @BindView(R.id.tv_danhao_text)
    TextView tv_danhao_text;


    @BindView(R.id.tv_bohui)
    TextView tv_bohui;
    @BindView(R.id.tv_tongyi)
    TextView tv_tongyi;
    @BindView(R.id.tv_beizhu)
    TextView tv_beizhu;

    @BindView(R.id.tv_jinshenhe)
    TextView tv_jinshenhe;


    @BindView(R.id.tv_chandi_text)
    TextView tv_chandi_text;
    @BindView(R.id.tv_dunyun_text)
    TextView tv_dunyun_text;

    @BindView(R.id.tv_xiaoshou_text)
    TextView tv_xiaoshou_text;
    @BindView(R.id.tv_wuliu_text)
    TextView tv_wuliu_text;


    @BindView(R.id.tv_chuantime_text)
    TextView tv_chuantime_text;

    private GoodsInfo mGoodsInfo;
    private GoodsModel mModel;


    @Override
    public int getLayoutId() {
        return R.layout.activity_findcar_only_look;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("单据详情");

        getToolbarStatus().isShowToolbar(false);
        mModel = new GoodsModel();

        Bundle bundle = getIntent().getExtras();
        String erpdetails = bundle.getString("toexamine");
        Gson gson=new Gson();
        mGoodsInfo = gson.fromJson(erpdetails, GoodsInfo.class);

        initData();
    }

    private void initData() {

        tvZhuanghuoText.setText(mGoodsInfo.deliverprovince+" "+mGoodsInfo.delivercity+" "+mGoodsInfo.delivercounty+" ");
        tvKufangText.setText(mGoodsInfo.housename);
        tvMudiText.setText(mGoodsInfo.destinationprovince+" "+mGoodsInfo.destinationcity+" "+mGoodsInfo.destinationcounty+" ");
        tvZhongliangText.setText(mGoodsInfo.quantity);

        tvHuoquanText.setText(mGoodsInfo.hq);
        tvTimeText.setText(mGoodsInfo.voucherdate);


        tv_danhao_text.setText(mGoodsInfo.code);
        tvDunweifeiText.setText(mGoodsInfo.inventoryname);
        tvDunzhuancheText.setText(mGoodsInfo.memo);
        tvDuncangchuText.setText(mGoodsInfo.remarks);
        tvQitaText.setText(mGoodsInfo.verifymessage);

        tv_chandi_text.setText(mGoodsInfo.cd);
        tv_dunyun_text.setText(mGoodsInfo.dyf);

        if ("0".equals(mGoodsInfo.status)){
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.black));
            tvStatusText.setText("待审核");

            tv_bohui.setVisibility(View.VISIBLE);
            tv_tongyi.setVisibility(View.VISIBLE);
//            tv_qrcode_scan.setVisibility(View.VISIBLE);
        }else if ("1".equals(mGoodsInfo.status)){
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.xui_btn_green_normal_color));
            tvStatusText.setText("已经审核");

            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.VISIBLE);
        }else if ("2".equals(mGoodsInfo.status)){
            tvStatusText.setText("退回");
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.colorRed));

            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.GONE);
        }else if ("99".equals(mGoodsInfo.status)){
            tvStatusText.setText("待确认");
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.colorRed));

            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.GONE);
        }else if ("98".equals(mGoodsInfo.status)){
            tvStatusText.setText("已确认");
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.colorRed));

            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.GONE);
        }else {
            tvStatusText.setText("作废");
            tvStatusText.setTextColor(getContext().getResources().getColor(R.color.colorRed));

            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.GONE);
        }

        int rolePosition = SharedPreferenceManager.getInstance().getRolePosition();
        if (2==rolePosition){
            //2 销售，3物流
            tv_bohui.setVisibility(View.GONE);
            tv_tongyi.setVisibility(View.GONE);
        }

        tv_xiaoshou_text.setText(mGoodsInfo.destinationusername);
        tv_wuliu_text.setText(mGoodsInfo.dispatcherusername);
        tv_chuantime_text.setText(mGoodsInfo.create_time);

//        tvStatusText.setText(mGoodsInfo.housename);

    }

    @Override
    public BaseFragment createFragment() {
        return null;
    }

    @OnClick(R.id.tv_bohui)
    public void bohui() {
        showTerminateDialog(this,mGoodsInfo);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }



    @OnClick(R.id.tv_tongyi)
    public void tongyi() {
        tongYiFaDan();

    }

    @OnClick(R.id.tv_jinshenhe)
    public void jinshenhe() {
        jinShenHe();

    }




    @OnClick({R.id.tv_beizhu,R.id.tv_qita_name,R.id.tv_qita_text})
    public void beizhu() {
        setXiangQingBeiZhu();
    }




//    @OnClick(R.id.activity_about_downloadTextView)
//    public void download() {
//        String url = AppConfig.apkUrl;
//        Uri uri = Uri.parse(url);
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//    }


    private void showTerminateDialog(Context context, GoodsInfo  mGoodsInfo) {
        new OperateTipDialog.Builder(context)
                .title("请输入驳回原因")
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("请输入驳回原因");
                            return;
                        }
                        dialog.dismiss();
                        boHuiZhaoChe(mGoodsInfo);

                    }
                })
                .show();
    }


    // COMMENT '状态新增 0 待审核、1已经审核 2退回,3作废'")
    //0 备注 1 已审 2 退回
    private void boHuiZhaoChe(GoodsInfo mGoodsInfo) {
//        Map<String, String> map = new HashMap<>();
//        map.put("id", mGoodsInfo.id);
//        map.put("status", "2");

        BoHuiBodyBean boHuiBodyBean=new BoHuiBodyBean(mGoodsInfo.id,"","2");

        mModel.boHuiZhaoChe(this, boHuiBodyBean, true, bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
//                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
//                    if (info != null) {
//                        SharedPreferenceManager.getInstance().setToken(info.token);
//                        EventBusManage.refreshConsignorInfo();
                        EventBusManage.refreshOrderList();
//                        EventBusManage.goYunDan();

                        finish();
                    }


            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });

//        mModel.boHuiZhaoChe(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
//                    if (info != null) {
////                        SharedPreferenceManager.getInstance().setToken(info.token);
////                        EventBusManage.refreshConsignorInfo();
//                        EventBusManage.refreshOrderList();
////                        EventBusManage.goYunDan();
//
//                        finish();
//                    }
//
//                } else {
//                    showToast(commonResponse.getMsg());
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });

    }


    private void setXiangQingBeiZhu() {
        new OperateTipDialog.Builder(this)
                .title("请输入备注")
                .input(new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("请输入备注");
                            return;
                        }
                        dialog.dismiss();
                        sendXiangQingBeiZhu(reason);

                    }
                })
                .show();

    }

    private void sendXiangQingBeiZhu(String reason) {

        BoHuiBodyBean boHuiBodyBean=new BoHuiBodyBean(mGoodsInfo.id,reason,"0");

        mModel.boHuiZhaoChe(this, boHuiBodyBean, true, bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    ToastUtil.showShortToast("添加备注成功");
                    mGoodsInfo.verifymessage=reason;
                    tvQitaText.setText(reason);
//                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
//                    if (info != null) {
//                        SharedPreferenceManager.getInstance().setToken(info.token);
//                        EventBusManage.refreshConsignorInfo();
//                        EventBusManage.refreshOrderList();
//                        EventBusManage.goYunDan();
//
//                        finish();
//                    }


                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });

//        Map<String, String> map = new HashMap<>();
//        map.put("id", mGoodsInfo.id);
//        map.put("verifymessage", reason);
//        map.put("status", "0");
//
//        mModel.boHuiZhaoChe(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    ToastUtil.showShortToast("添加备注成功");
//                    mGoodsInfo.verifymessage=reason;
//                    tvQitaText.setText(reason);
////                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
////                    if (info != null) {
////                        SharedPreferenceManager.getInstance().setToken(info.token);
////                        EventBusManage.refreshConsignorInfo();
////                        EventBusManage.refreshOrderList();
////                        EventBusManage.goYunDan();
////
////                        finish();
////                    }
//
//
//                } else {
//                    showToast(commonResponse.getMsg());
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });

    }


    private void tongYiFaDan() {


        BoHuiBodyBean boHuiBodyBean=new BoHuiBodyBean(mGoodsInfo.id,"同意","1");

        mModel.boHuiZhaoChe(this, boHuiBodyBean, true, bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {

                    Gson gson=new Gson();
                    String json = gson.toJson(mGoodsInfo);
                    Bundle bundle=new Bundle();
                    bundle.putString("toexamine",json);
                    openActivity(CarSearchAuditActivity.class, bundle);

                    finish();

//                    String msg = commonResponse.getMsg();
//                    ToastUtil.showShortToast("添加备注成功");
//                    mGoodsInfo.verifymessage=reason;
//                    tvQitaText.setText(reason);
//                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
//                    if (info != null) {
//                        SharedPreferenceManager.getInstance().setToken(info.token);
//                        EventBusManage.refreshConsignorInfo();
//                        EventBusManage.refreshOrderList();
//                        EventBusManage.goYunDan();
//
//                        finish();
//                    }


                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });

//        Map<String, String> map = new HashMap<>();
//        map.put("id", mGoodsInfo.id);
//        map.put("status", "1");
//
//        mModel.boHuiZhaoChe(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse commonResponse) {
//                if (commonResponse.getCode() == 20000) {
////                    LoginInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), LoginInfo.class);
////                    if (info != null) {
////                        SharedPreferenceManager.getInstance().setToken(info.token);
////                        EventBusManage.refreshConsignorInfo();
////                        EventBusManage.refreshOrderList();
////                        EventBusManage.goYunDan();
////
////                        finish();
////                    }
//
//                } else {
//                    showToast(commonResponse.getMsg());
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });
    }


    private void jinShenHe() {
        BoHuiBodyBean boHuiBodyBean=new BoHuiBodyBean(mGoodsInfo.id,"同意","1");

        mModel.boHuiZhaoChe(this, boHuiBodyBean, true, bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {

//                    Gson gson=new Gson();
//                    String json = gson.toJson(mGoodsInfo);
//                    Bundle bundle=new Bundle();
//                    bundle.putString("toexamine",json);
//                    openActivity(CarSearchAuditActivity.class, bundle);
//                    EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_REFRESH_ORDER_LIST.getValue()));
                    EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_REFRESH_ORDER_LIST.getValue()));
                    finish();

                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                setMsg(e.message);
            }
        });
    }




}
